<template>
  <div>
    <!--基础表新增-->
    <el-card>
      <el-form :model="xmData" :inline="true" label-width="80px" size="small">
        <!--基础表新增-->
        <el-row>
          <el-col :span="8">
            <el-form-item label="客户名称">
              <el-input v-model="xmData.entName" style="width:200px" disabled></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="报表类型">
              <el-select v-model="xmData.typeFlag" placeholder="请选择报表类型">
                <el-option label="全部" value=""></el-option>
                <el-option label="资产负债表" value="1"></el-option>
                <el-option label="损益表" value="2"></el-option>
                <el-option label="现金流量表" value="3"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="2">
            <el-button type="primary" size="small" icon="el-icon-search" @click="init">查 询</el-button>
          </el-col>
          <el-col :span="2">
            <el-button type="primary" size="small" icon="el-icon-circle-plus-outline" @click="add">新 增</el-button>
          </el-col>
          <el-col :span="2">
            <el-dropdown>
              <el-button type="primary" size="small" icon="el-icon-upload">导 入</el-button>
              <el-dropdown-menu slot="dropdown">
                <el-dropdown-item @click.native="goImportNbsj()">内部数据</el-dropdown-item>
                <el-dropdown-item @click.native="goImport()">外部数据</el-dropdown-item>
              </el-dropdown-menu>
            </el-dropdown>
            <!-- <el-button type="primary" size="small" icon="el-icon-upload" @click="goImport">导 入</el-button> -->
          </el-col>
          <el-col :span="2">
            <el-button type="success" size="small" icon="el-icon-paperclip" @click="back">返 回</el-button>
          </el-col>
        </el-row>
      </el-form>
    </el-card>

    <el-table :data="tableData" border style="width: 100%;" size="mini">
      <el-table-column type="index" label="序号" align="center" width="50"></el-table-column>
      <el-table-column property="collectCode" width="270" label="财务报表编号"></el-table-column>
      <el-table-column property="collectName" label="财务报表名称"></el-table-column>
      <el-table-column property="periodDate" label="会计期间"></el-table-column>
      <el-table-column property="stepEnumid" label="数据采集阶段"></el-table-column>
      <el-table-column property="typeFlag" label="报表类型"></el-table-column>
      <el-table-column label="操作" align="center" width="200">
        <template slot-scope="scope">
          <el-button @click="toEdit(scope.row)" type="text" size="mini">编辑</el-button>
          <el-button @click="toEditXxmx(scope.row)" type="text" size="mini">信息明细</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-card style="text-align:right;">
      <el-pagination @size-change="sizeChange" @current-change="currentChange" :current-page="currentPage" :page-sizes="[ 10, 20,30, 40]" :page-size="pageSize" background layout="total, sizes, prev, pager, next" :total="total"></el-pagination>
    </el-card>
    <el-dialog title="上传财务数据" :visible.sync="dialogScVisible" append-to-body>
      <el-row>
        <el-col :span="5">
          数据采集阶段：
        </el-col>
        <el-col :span="8">
          <el-select v-model="stepEnumid" placeholder="请选择报表类型" size="small">
            <el-option label="企业财务信息" value="1"></el-option>
            <el-option label="A角调查财务信息" value="2"></el-option>
            <el-option label="保后调查财务信息" value="3"></el-option>
          </el-select>
        </el-col>
      </el-row>
      <el-row style="margin-top:10px">
        <el-col :span="5">
          上传财务数据：
        </el-col>
        <el-col :span="8">
          <el-upload multiple style="display:inline-block" class="upload-demo" ref="upload" action="/hqx/knowledge/importKnowledge" :file-list="fileList" :http-request="uploadSectionFile" :auto-upload="false">
            <el-button slot="trigger" size="mini" type="primary" plain>选取文件</el-button>
          </el-upload>
        </el-col>
      </el-row>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogScVisible = false">取 消</el-button>
        <el-button type="primary" @click="submitUpload">保存</el-button>
      </div>
    </el-dialog>

    <el-dialog title="编辑财务信息" :visible.sync="dialogBjCwxxVisible" :close-on-click-modal="false">
      <el-form :model="cwxxFormData" label-width="120px" size="mini">
        <el-form-item label="客户名称" size="small">
          <el-input v-model="cwxxFormData.corpName" disabled></el-input>
        </el-form-item>
        <el-form-item label="财务报表编号" size="small">
          <el-input v-model="cwxxFormData.collectCode" disabled></el-input>
        </el-form-item>
        <el-form-item label="财务报表名称" size="small">
          <el-input v-model="cwxxFormData.collectName" placeholder="请输入报表模板名称"></el-input>
        </el-form-item>
        <el-form-item label="会计期间" size="small">
          <el-date-picker v-model="cwxxFormData.periodDate" value-format="yyyyMM" type="month" placeholder="选择月">
          </el-date-picker>
          <!-- <el-input v-model="cwxxFormData.periodDate" placeholder="请输入报表模板名称"></el-input> -->
        </el-form-item>
        <el-form-item label="数据采集阶段" size="small">
          <el-select v-model="cwxxFormData.stepEnumid" placeholder="请选择报表类型" size="small">
            <el-option label="企业财务信息" value="1"></el-option>
            <el-option label="A角调查财务信息" value="2"></el-option>
            <el-option label="保后调查财务信息" value="3"></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogBjCwxxVisible = false">取 消</el-button>
        <el-button type="primary" @click="doSaveCwxx">确 定</el-button>
      </div>
    </el-dialog>

    <el-dialog title="" :visible.sync="dialogBjVisible" :close-on-click-modal="false" width="900px">
      <el-tabs type="border-card">
        <el-tab-pane label="财务信息">
          <el-form :model="cwxxFormData" label-width="120px" size="mini">
            <el-row>
              <el-col :span="8">
                <el-form-item label="客户名称" size="small">
                  <el-input v-model="cwxxFormData.corpName" placeholder="请输入客户名称"></el-input>
                </el-form-item>
              </el-col>
              <el-col :span="8">
                <el-form-item label="财务报表编号" size="small">
                  <el-input v-model="cwxxFormData.collectCode" disabled=""></el-input>
                </el-form-item>
              </el-col>
              <el-col :span="8">
                <el-form-item label="财务报表名称" size="small">
                  <el-input v-model="cwxxFormData.collectName" placeholder="请输入报表模板名称"></el-input>
                </el-form-item>
              </el-col>
            </el-row>
            <el-row>
              <el-col :span="8">
                <el-form-item label="会计期间" size="small">
                  <el-input v-model="cwxxFormData.periodDate" placeholder="请输入报表模板名称"></el-input>
                </el-form-item>
              </el-col>
              <el-col :span="8">
                <el-form-item label="数据采集阶段" size="small">
                  <el-select v-model="cwxxFormData.stepEnumid" placeholder="请选择报表类型" size="small">
                    <el-option label="企业财务信息" value="1"></el-option>
                    <el-option label="A角调查财务信息" value="2"></el-option>
                    <el-option label="保后调查财务信息" value="3"></el-option>
                  </el-select>
                </el-form-item>
              </el-col>
            </el-row>
          </el-form>
        </el-tab-pane>
      </el-tabs>
      <el-tabs type="border-card">
        <el-tab-pane label="财务信息明细">
          <el-table :data="cwmxTableData" border style="width: 100%;" size="mini">
            <el-table-column type="index" label="序号" align="center" width="50"></el-table-column>
            <el-table-column property="subCode" label="科目编号"></el-table-column>
            <el-table-column property="subName" label="科目名称"></el-table-column>
            <el-table-column property="valueNum" label="数值(元)">
            </el-table-column>
            <el-table-column label="操作" align="center">
              <template slot-scope="scope">
                <el-button icon="el-icon-edit" circle @click="toEditKmxx(scope.row)" size="mini"></el-button>
              </template>
            </el-table-column>
          </el-table>
        </el-tab-pane>

      </el-tabs>
    </el-dialog>
    <el-dialog title="编辑" :visible.sync="dialogBjKmxxVisible" :close-on-click-modal="false">
      <el-form :model="mxform">
        <el-form-item label="数值">
          <el-input v-model="mxform.valueNum" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogBjKmxxVisible = false">取 消</el-button>
        <el-button type="primary" @click="doSaveKmmx">确 定</el-button>
      </div>
    </el-dialog>

    <el-dialog title="新增财务信息" :visible.sync="dialogAddVisible" :close-on-click-modal="false" width="900px">
      <el-button type="primary" @click="doAddSaveCw" size="mini">保 存</el-button>

      <el-tabs type="border-card">
        <el-tab-pane label="财务信息">
          <el-form :model="addFormData" label-width="120px" size="mini">
            <el-row>
              <el-col :span="8">
                <el-form-item label="客户名称" size="small">
                  <el-input v-model="addFormData.corpName" placeholder="请输入客户名称"></el-input>
                </el-form-item>
              </el-col>
              <el-col :span="8">
                <el-form-item label="报表模板名称" size="small">
                  <el-select v-model="tmpId" placeholder="请选择" @change="selectGet">
                    <el-option v-for="item in tempList" :key="item.id" :label="item.tmpName" :value="item.id">
                    </el-option>
                  </el-select>
                </el-form-item>
              </el-col>
              <el-col :span="8">
                <el-form-item label="报表类型" size="small">
                  {{tmpTypeName}}
                </el-form-item>
              </el-col>
            </el-row>
            <el-row>
              <el-col :span="8">
                <el-form-item label="数据采集阶段" size="small">
                  <el-select v-model="addFormData.stepEnumid" placeholder="请选择报表类型" size="small">
                    <el-option label="企业财务信息" value="1"></el-option>
                    <el-option label="A角调查财务信息" value="2"></el-option>
                    <el-option label="保后调查财务信息" value="3"></el-option>
                  </el-select>
                </el-form-item>
              </el-col>
              <el-col :span="8">
                <el-form-item label="财务报表名称" size="small">
                  <el-input v-model="addFormData.collectName" placeholder="请输入报表模板名称"></el-input>
                </el-form-item>
              </el-col>
              <el-col :span="8">
                <el-form-item label="会计期间" size="small">
                  <el-date-picker v-model="addFormData.periodDate" style="width:150px" value-format="yyyyMM" type="month" placeholder="选择月">
                  </el-date-picker>
                </el-form-item>
              </el-col>

            </el-row>
          </el-form>
        </el-tab-pane>
      </el-tabs>
      <el-tabs type="border-card">
        <el-tab-pane label="财务信息明细">
          <el-table :data="detailTableData" border style="width: 100%;" size="mini">
            <el-table-column type="index" label="序号" align="center" width="50"></el-table-column>
            <el-table-column property="subName" align="center" label="科目名称"></el-table-column>
            <el-table-column property="valueNum" align="center" label="数值">
              <template slot-scope="scope">
                <el-input v-model="scope.row.valueNum" type="number" style="width:200px" size="mini" placeholder="请输入数值"></el-input>
              </template>
            </el-table-column>
          </el-table>
        </el-tab-pane>

      </el-tabs>
    </el-dialog>
    <el-dialog title="内部数据信息" :visible.sync="diaNbsjVisible" :close-on-click-modal="false" width="900px">
      <el-button type="primary" @click="doSaveNbsj" size="mini">保 存</el-button>
      <el-table :data="nbtableData" ref="nbtableData" @selection-change="handleSelectionChange" border style="width: 100%;" size="mini">
        <el-table-column type="selection" width="55"></el-table-column>
        <el-table-column type="index" label="序号" align="center" width="50"></el-table-column>
        <el-table-column property="collectCode" width="270" label="财务报表编号"></el-table-column>
        <el-table-column property="collectName" label="财务报表名称"></el-table-column>
        <el-table-column property="periodDate" label="会计期间"></el-table-column>
        <el-table-column property="stepEnumid" label="数据采集阶段"></el-table-column>
        <el-table-column property="typeFlag" label="报表类型"></el-table-column>
      </el-table>
      <el-card style="text-align:right;">
        <el-pagination @size-change="nbsizeChange" @current-change="nbcurrentChange" :current-page="nbcurrentPage" :page-sizes="[ 10, 20,30, 40]" :page-size="nbpageSize" background layout="total, sizes, prev, pager, next" :total="nbtotal"></el-pagination>
      </el-card>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    return {
      diaNbsjVisible: false,
      nbtableData: [],
      nbtotal: 0,
      nbpageSize: 10,
      nbcurrentPage: 1,
      nbCollectList: [],

      dialogAddVisible: false,
      tmpName: "",
      tmpId: "",
      tmpTypeName: "",
      addFormData: {
        corpName: "",
        collectCode: "",
        collectName: "",
        periodDate: "",
        stepEnumid: "",
        projectCode: ""
      },
      detailTableData: [],

      dialogBjCwxxVisible: false,
      cwxxFormData: {
        id: "",
        corpName: "",
        collectCode: "",
        collectName: "",
        periodDate: "",
        stepEnumid: ""
      },
      cwmxTableData: [],
      dialogBjKmxxVisible: false,
      mxform: {
        id: "",
        valueNum: ""
      },

      dialogBjVisible: false,
      fileList: [],
      dialogScVisible: false,
      tableData: [],
      stepEnumid: "2",
      total: 0,
      pageSize: 10,
      currentPage: 1,

      xmData: {
        projCode: "", //项目code
        entNo: "",
        entName: "",
        projName: "",
        typeFlag: ""
      },
      projectId: this.$route.query.projectId,
      tempList: []
    };
  },
  mounted() {
    this.getXmxx();
    this.getTemp();
    //this.init();
  },
  methods: {
    /****************************内部数据******************************************** */
    handleSelectionChange(val) {
      this.nbCollectList = val;
    },
    doSaveNbsj() {
      if (this.nbCollectList == "") {
        this.$message.error("请选择数据");
        return;
      }
      let finDateCollect = {
        projectCode: this.xmData.projCode
      };
      let params = {
        finDateCollect: finDateCollect,
        finDateCollectList: this.nbCollectList
      };
      this.$api
        .post(  "/api/biz/finTest/doSubmitNbsj", params)
        .then(res => {
          if (res.retHead.code === "0000000") {
            this.init();
            this.$message({
              type: "success",
              message: "编辑成功！"
            });
            this.diaNbsjVisible = false;
          }
        });
    },
    goImportNbsj() {
      this.nbinit();
      this.diaNbsjVisible = true;
    },
    //改编页码方法
    nbsizeChange(n) {
      this.nbpageSize = n;
      this.nbinit();
    },
    nbcurrentChange(n) {
      this.nbcurrentPage = n;
      this.nbinit();
    },
    nbinit() {
      let params = {
        pageNum: this.nbcurrentPage,
        pageSize: this.nbpageSize,
        corpCode: this.xmData.entNo
      };
      this.$api
        .formPost(  "/api/biz/finTest/doGetList", params)
        .then(res => {
          if (res.retHead.code === "0000000") {
            this.nbtableData = res.retBody.list;
            console.log(this.nbtableData);
            this.nbtotal = res.retBody.total;
            this.nbcurrentPage = res.retBody.pageNum;
          }
        });
    },
    /****************************保存方法******************************************** */
    doAddSaveCw() {
      if (this.tmpName == "") {
        this.$message.error("请选择对应的模板");
        return;
      }
      if (this.addFormData.periodDate == "") {
        this.$message.error("请选择会计期间");
        return;
      }
      let finDateCollect = {
        corpCode: this.addFormData.corpCode,
        corpName: this.addFormData.corpName,
        stepEnumid: this.addFormData.stepEnumid,
        projectCode: this.addFormData.projectCode,
        periodDate: this.addFormData.periodDate,
        tmpId: this.tmpId,
        tmpName: this.tmpName,
        collectName: this.addFormData.collectName
      };
      let params = {
        finDateCollect: finDateCollect,
        finCollectDetailList: this.detailTableData
      };
      this.$api
        .post(  "/api/biz/finTest/doSubmit", params)
        .then(res => {
          if (res.retHead.code === "0000000") {
            this.init();
            this.$message({
              type: "success",
              message: "编辑成功！"
            });
            this.dialogAddVisible = false;
          }
        });
    },
    //模板下拉选择
    selectGet(vId) {
      let obj = {};
      obj = this.tempList.find(item => {
        //这里的tempList就是上面遍历的数据源
        return item.id === vId; //筛选出匹配数据
      });
      console.log(obj.tmpName); //我这边的name就是对应label的
      this.tmpName = obj.tmpName;
      this.addFormData.collectName = obj.tmpName;
      if (obj.tmpType == "0") {
        this.tmpTypeName = "月报";
      } else {
        this.tmpTypeName = "年报";
      }
      let params = {
        tmpId: vId
      };
      this.$api
        .formPost(  "/api/biz/finTest/doGetTmpMx", params)
        .then(res => {
          if (res.retHead.code === "0000000") {
            this.detailTableData = res.retBody;
          }
        });
    },
    //获取模板信息
    getTemp() {
      let params = {};
      this.$api
        .post(  "/api/biz/finTemplate/doGetList", params)
        .then(res => {
          if (res.retHead.code === "0000000") {
            this.tempList = res.retBody;
            console.log(this.tempList);
          }
        });
    },

    //新增方法
    add() {
      this.detailTableData = [];
      this.addFormData.corpCode = this.xmData.entNo;
      this.addFormData.corpName = this.xmData.entName;
      this.addFormData.collectName = "";
      this.addFormData.stepEnumid = "2";
      this.addFormData.projectCode = this.xmData.projCode;
      this.addFormData.periodDate = "";

      this.dialogAddVisible = true;
    },

    /***********************************编辑信息明细相关*********************************** */
    //编辑科目信息明细
    toEditKmxx(row) {
      this.mxform.valueNum = row.valueNum;
      this.mxform.id = row.id;
      this.dialogBjKmxxVisible = true;
    },
    //确定保存kmmx
    doSaveKmmx() {
      if (this.mxform.valueNum == "") {
        this.$message.error("请输入正确的数值");
        return;
      }
      let params = {
        id: this.mxform.id,
        valueNum: this.mxform.valueNum
      };
      this.$api
        .formPost(  "/api/biz/finTest/doModCwKmmx", params)
        .then(res => {
          if (res.retHead.code === "0000000") {
            this.cwmxTableData = res.retBody;
            this.getKmxxList();
            this.dialogBjKmxxVisible = false;
          }
        });
    },
    //获取科目信息list
    getKmxxList() {
      let params = {
        collectId: this.cwxxFormData.id
      };
      this.$api
        .formPost(  "/api/biz/finTest/doGetCwxxMx", params)
        .then(res => {
          if (res.retHead.code === "0000000") {
            this.cwmxTableData = res.retBody;
          }
        });
    },
    //编辑信息明细
    toEditXxmx(row) {
      this.cwxxFormData.id = row.id;
      this.cwxxFormData.corpName = row.corpName;
      this.cwxxFormData.collectCode = row.collectCode;
      this.cwxxFormData.collectName = row.collectName;
      this.cwxxFormData.periodDate = row.periodDate;
      this.cwxxFormData.stepEnumid = row.stepEnumid;
      let params = {
        collectId: row.id
      };
      this.$api
        .formPost(  "/api/biz/finTest/doGetCwxxMx", params)
        .then(res => {
          if (res.retHead.code === "0000000") {
            this.cwmxTableData = res.retBody;
            this.dialogBjVisible = true;
            console.log(this.cwmxTableData);
          }
        });
    },
    /*************************************编辑财务信息********************************* */
    //编辑财务信息
    toEdit(row) {
      this.cwxxFormData.id = row.id;
      this.cwxxFormData.corpName = row.corpName;
      this.cwxxFormData.collectCode = row.collectCode;
      this.cwxxFormData.collectName = row.collectName;
      this.cwxxFormData.periodDate = row.periodDate;
      this.cwxxFormData.stepEnumid = row.stepEnumid;
      this.dialogBjCwxxVisible = true;
    },
    doSaveCwxx() {
      let params = {
        id: this.cwxxFormData.id,
        periodDate: this.cwxxFormData.periodDate,
        collectName: this.cwxxFormData.collectName,
        stepEnumid: this.cwxxFormData.stepEnumid
      };
      this.$api
        .formPost(  "/api/biz/finTest/doModCwxx", params)
        .then(res => {
          if (res.retHead.code === "0000000") {
            this.cwmxTableData = res.retBody;
            this.init();
            this.$message({
              type: "success",
              message: "编辑成功！"
            });
            this.dialogBjCwxxVisible = false;
          }
        });
    },
    /**************************************主页面方法******************************** */
    //获取项目信息
    getXmxx() {
      this.$api
        .formPost(  "/api/biz/finTest/doGetEntAndXmxx", {
          projId: this.projectId
        })
        .then(res => {
          if (res.retHead.code == "0000000" && res.retBody != null) {
            this.xmData.entNo = res.retBody.entNo;
            this.xmData.entName = res.retBody.entName;
            this.xmData.projName = res.retBody.projName;
            this.xmData.projCode = res.retBody.projCode;
            let params = {
              pageNum: this.currentPage,
              pageSize: this.pageSize,
              typeFlag: this.xmData.typeFlag,
              projectCode: this.xmData.projCode
            };
            this.$api
              .formPost(  "/api/biz/finTest/doGetList", params)
              .then(res => {
                if (res.retHead.code === "0000000") {
                  this.tableData = res.retBody.list;
                  this.total = res.retBody.total;
                  this.currentPage = res.retBody.pageNum;
                }
              });
          } else {
            this.$message.error("查询项目信息为空，请刷新重试");
          }
        });
    },
    init() {
      let params = {
        pageNum: this.currentPage,
        pageSize: this.pageSize,
        typeFlag: this.xmData.typeFlag,
        projectCode: this.xmData.projCode
      };
      this.$api
        .formPost(  "/api/biz/finTest/doGetList", params)
        .then(res => {
          if (res.retHead.code === "0000000") {
            this.tableData = res.retBody.list;
            this.total = res.retBody.total;
            this.currentPage = res.retBody.pageNum;
          }
        });
    },
    /**************************************导入外部数据******************************** */
    goImport() {
      this.dialogScVisible = true;
    },
    submitUpload() {
      let list = document.getElementsByClassName(
        "el-upload-list__item is-ready"
      );
      if (list.length == 0) {
        this.$message({
          type: "warning",
          message: "请选择需要上传的模板！"
        });
        return;
      }
      this.$refs.upload.submit();
    },
    //请求方法
    uploadSectionFile(param) {
      var fileObj = param.file;
      let fdata = new FormData();
      // 文件对象
      fdata.append("file", fileObj);
      fdata.append("projectCode", this.xmData.projCode);
      fdata.append("corpCode", this.xmData.entNo);
      fdata.append("corpName", this.xmData.entName);
      fdata.append("stepEnumid", this.stepEnumid);
      this.$api
        .formDataPost(  "/api/biz/finTest/doSaveExcel", fdata)
        .then(res => {
          if (res.retHead.code == "0000000") {
            this.dialogScVisible = false;
            this.fileList = [];
            this.init();
          } else {
            this.$Message.error("上传失败，失败原因为" + res.retBody);
            return;
          }
        });
    },
    /********************************************************************** */
    //改编页码方法
    sizeChange(n) {
      this.pageSize = n;
      this.init();
    },
    currentChange(n) {
      this.currentPage = n;
      this.init();
    },
    //返回按钮
    back() {
      this.$router.go(-1); //返回上一层
    }
  }
};
</script>
<style lang='scss' scoped>
</style>